package com.wpw.java8demo;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Stream;

import org.junit.Test;

public class StreamMakeTest {
	
	@Test
	public void test1() {
		Stream<String> stream = Stream.of("hello", "world");
		stream.map(String::toUpperCase)
		.forEach(System.out::println);
	}

	@Test
	public void test2() {
		Integer[] arrays = {1,2,3,4};
		Stream.of(arrays)
			.forEach(System.out::println);
	}
	
	@Test
	public void test3() {
		long count = 0;
		try(Stream<String> lines = Files.lines(Paths.get("data.txt"), Charset.defaultCharset())) {
			count = lines.flatMap(line -> Arrays.stream(line.split(" ")))
				.distinct()
				.count();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(count);
	}
	
	@Test
	public void test4() {
		Stream.iterate(0, n -> n + 1)
			.limit(7)
			.forEach(System.out::println);
	}
	
	@Test
	public void test5() {
		Stream.iterate(new int[]{0, 1},t -> new int[]{t[1], t[0] + t[1]})
			.limit(20)
			.map(t -> t[0])
			.forEach(System.out::println);
	}
	
	@Test
	public void test6() {
		Stream.generate(() -> 2)
			.limit(10)
			.forEach(System.out::println);
	}
}
